package com.wyx.videoplay.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wyx.videoplay.AddCustormActivity;
import com.wyx.videoplay.R;
import com.wyx.videoplay.entity.DevicesInfo;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link DeviceFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class DeviceFragment extends Fragment {

    private TextView tvRightText;
    private TextView tvTitle;
    private RecyclerView mRvDeviceList;

    // TODO: Rename and change types and number of parameters
    public static DeviceFragment newInstance() {
        DeviceFragment fragment = new DeviceFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_device, container, false);
        initView(view);
        return view;
    }

    private void initView(View view){
        view.findViewById(R.id.iv_back_layout_top).setVisibility(View.INVISIBLE);
        tvRightText = view.findViewById(R.id.iv_option2_layout_top);
        tvTitle = view.findViewById(R.id.tv_title_layout_top);
        tvTitle.setText("设备信息");
        tvRightText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), AddCustormActivity.class));
            }
        });

        mRvDeviceList = view.findViewById(R.id.rv_device_list);
        mRvDeviceList.setLayoutManager(new GridLayoutManager(getActivity(),4));
        List<DevicesInfo> datas=new ArrayList<>();
        DevicesInfo devicesInfo = new DevicesInfo();
        devicesInfo.setName("wuyunxiang");
        datas.add(devicesInfo);
        if(datas.size()<4){
            datas.add(new DevicesInfo());
        }
        CommonAdapter<DevicesInfo> commonAdapter = new CommonAdapter<DevicesInfo>(getActivity(), R.layout.device_info, datas) {

            @Override
            protected void convert(ViewHolder holder, DevicesInfo devicesInfo, int position) {

                if(TextUtils.isEmpty(devicesInfo.getName())){
                    holder.setVisible(R.id.tv_name,false);
                    holder.setVisible(R.id.tv_ip,false);
                    holder.setVisible(R.id.tv_port,false);
                    holder.setVisible(R.id.iv_add_device,true);
                }else {
                    holder.setVisible(R.id.tv_name,true);
                    holder.setVisible(R.id.tv_ip,true);
                    holder.setVisible(R.id.tv_port,true);
                    holder.setVisible(R.id.iv_add_device,false);

                    holder.setText(R.id.tv_name,"摄像头一号");
                    holder.setText(R.id.tv_ip,"IP：asdfasdf");
                    holder.setText(R.id.tv_port,"端口：阿斯蒂芬自行车v");

                }

            }
        };

        mRvDeviceList.setAdapter(commonAdapter);
        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder viewHolder, int i) {

            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder viewHolder, int i) {
                return false;
            }
        });

    }

}
